<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>

<%@ page import="com.conversationboard.site.*" %>
<%@ page import="com.conversationboard.config.*" %>
<%@ page import="com.conversationboard.model.StyleSheets"%>

<%@ page pageEncoding="UTF-8"%>
<%@ page contentType="text/html;charset=UTF-8"%>

<%@ page import="com.conversationboard.view.MainMenu"%>
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
		<title>Cache Statistics</title>
		<link rel="stylesheet"title="Conversation Board" type="text/css" href="<%=Configuration.getInstance().getRoot()%>/Stylesheets/<%=StyleSheets.getStyleSheetFileName(request.getCookies())%>">
	</head>

	<body>
	
		<%= MainMenu.display(request.getUserPrincipal()) %>
	
		<div class="headingblock">
			<h1>Conversation Board Performance Statistics</h1>
		</div>

		<div class="mainblock">

			<div class="formsectionlight">

				<h2>High Level</h2>
	
				<table class="table">
					
					<tr class="header">
						<td class="cell">Total Page Hits</td>
						<td class="cell">Total "Show All Messages" Page Hits</td>
						<td class="cell">Database Connections</td>
						<td class="cell">Cache Hits</td>
						<td class="cell">Cache Misses</td>
						<td class="cell">Max Memory</td>
						<td class="cell">Used Memory</td>
						<td class="cell">Free Memory</td>
					</tr>			
				
					<tr class="row">
						<td class="cell"><fmt:formatNumber>${pageHitCount}</fmt:formatNumber></td>
						<td class="cell"><fmt:formatNumber>${showAllCount}</fmt:formatNumber> (<fmt:formatNumber maxFractionDigits="2">${showAllCount/pageHitCount * 100}</fmt:formatNumber>%)</td>
						<td class="cell"><fmt:formatNumber>${totalCount}</fmt:formatNumber></td>
						<td class="cell"><fmt:formatNumber><%= Site.getCacheHitCount() %></fmt:formatNumber></td>
						<td class="cell"><fmt:formatNumber><%= Site.getCacheMissCount() %></fmt:formatNumber></td>
						<td class="cell"><fmt:formatNumber maxFractionDigits="2">${maxMemory/1000000}</fmt:formatNumber>M</td>
						<td class="cell"><fmt:formatNumber maxFractionDigits="2">${(totalMemory - freeMemory)/1000000}</fmt:formatNumber>M</td>
						<td class="cell"><fmt:formatNumber maxFractionDigits="2">${(maxMemory - (totalMemory - freeMemory))/1000000}</fmt:formatNumber>M</td>
					</tr>
					
				</table>
			</div>
			
			<br />
	
			<div class="formsectionlight">
				<h2>Database Connection Breakdown</h2>
	
				<p>Whenever Conversation Board does not get its information from the cache, it goes to the database, which has a significantly
				   higher overhead. This table shows you the methods that requested a connection to the database and the times associated with
				   the use of that connection.</p>
		
				<p>Note that this information will only be present if the <strong>StoreDatabaseConnectionInfo</strong> node is set to <strong>true</strong> in the site configuration.</p>
	
				<h3>Timings measured in milliseconds</h3>
	
				<table class="table">
					
					<tr class="header">
						<td class="cell">Caller Class</td>
						<td class="cell">Count</td>
						<td class="cell">Total Time (ms)</td>
						<td class="cell">Average Time (ms)</td>
						<td class="cell">Min Time (ms)</td>
						<td class="cell">Max Time (ms)</td>
					</tr>			
				
					<c:forEach var="performanceRecord" items="${performanceRecords}">
						<tr class="row">
							<td class="cell">${performanceRecord.callerName}</td>
							<td class="cell">${performanceRecord.count}</td>
							<td class="cell"><fmt:formatNumber type="number" maxFractionDigits="2">${performanceRecord.totalTime / 1000}</fmt:formatNumber></td>
							<td class="cell"><fmt:formatNumber type="number" maxFractionDigits="2">${performanceRecord.averageTime / 1000}</fmt:formatNumber></td>
							<td class="cell"><fmt:formatNumber type="number" maxFractionDigits="2">${performanceRecord.minTime / 1000}</fmt:formatNumber></td>
							<td class="cell"><fmt:formatNumber type="number" maxFractionDigits="2">${performanceRecord.maxTime / 1000}</fmt:formatNumber></td>
						</tr>
					</c:forEach>
					
				</table>
				
				<p><strong>Total Count:</strong> <fmt:formatNumber type="number" maxFractionDigits="2">${totalCount}</fmt:formatNumber></p>
				<p><strong>Total Time (ms):</strong> <fmt:formatNumber type="number" maxFractionDigits="2">${totalTime / 1000}</fmt:formatNumber></p>
				<p><strong>Server Was Started:</strong> ${serverUpTime}</p>

			</div>
			
			<br />
				
				<c:if test="${user.siteAdmin}">
					<div class="formsection">
						<h2>Cache Eviction Rate and Database Connection Rate</h2>
					
						<table class="table">
							
							<tr class="header">
								<td class="cell">Measure</td>
								<td class="cell">Rate</td>
							</tr>			
						
							<tr class="row">
								<td class="cell">User Cache Evictions per Second</td>
								<td class="cell">${userCacheEvictions / secondsSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">User Cache Evictions per Minute</td>
								<td class="cell">${userCacheEvictions / minutesSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">User Cache Evictions per Hour</td>
								<td class="cell">${userCacheEvictions / hoursSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">Thread Cache Evictions per Second</td>
								<td class="cell">${threadCacheEvictions / secondsSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">Thread Cache Evictions per Minute</td>
								<td class="cell">${threadCacheEvictions / minutesSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">Thread Cache Evictions per Hour</td>
								<td class="cell">${threadCacheEvictions / hoursSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">Database Connections per Second</td>
								<td class="cell">${totalCount / secondsSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">Database Connections per Minute</td>
								<td class="cell">${totalCount / minutesSinceStartup}</td>
							</tr>
							<tr class="row">
								<td class="cell">Database Connections per Hour</td>
								<td class="cell">${totalCount / hoursSinceStartup}</td>
							</tr>
						</table>
					</div>
				</c:if>

			<br />

			<c:if test="${user.siteAdmin}">
	
				<div class="formsectionlight">

					<h2>Database Info</h2>
					
					<br />
		
					<h3>Current Threads</h3>
			
					<table class="table">
						
						<tr class="header">
							<td class="cell">ID</td>
							<td class="cell">User</td>
							<td class="cell">Host</td>
							<td class="cell">Database</td>
							<td class="cell">Command</td>
							<td class="cell">Time</td>
							<td class="cell">State</td>
							<td class="cell">Info</td>
						</tr>			
					
						<c:forEach var="thread" items="${databaseThreads}">
							<tr class="row">
								<td class="cell">${thread.id}</td>
								<td class="cell">${thread.user}</td>
								<td class="cell">${thread.host}</td>
								<td class="cell">${thread.database}</td>
								<td class="cell">${thread.command}</td>
								<td class="cell">${thread.time}</td>
								<td class="cell">${thread.state}</td>
								<td class="cell">${thread.info}</td>
							</tr>
						</c:forEach>
						
					</table>
			
					<br />
			
					<h3>Info</h3>
			
					<table class="table">
						
						<tr class="header">
							<td class="cell">Variable Name</td>
							<td class="cell">Value</td>
						</tr>			
					
						<c:forEach var="statusItem" items="${status}">
							<tr class="row">
								<td class="cell">${statusItem.key}</td>
								<td class="cell">${statusItem.value}</td>
							</tr>
						</c:forEach>
						
					</table>
				</div>
			</c:if>
		</div>	


		<!--<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>-->
		<!--<script type="text/javascript">_uacct = "UA-1148679-1";urchinTracker();</script>--> 
		
	</body>
	
</html>
